import { createApp } from 'vue';
import App from './App.vue';
import './assets/global.less';
import components from './components/global';
import Router from './router/index';

const app = createApp(App)
app.config.productionTip = false
// ************************************自用组件库************************************
// 饿了么ui
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import 'element-plus/dist/index.css'
app.use(ElementPlus, {
  locale: zhCn,
})
// 饿了么ui图标库
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
for (const i in ElementPlusIconsVue) {
  app.component(i, ElementPlusIconsVue[i])
}
// 饿了么ui封住库-avue
import Avue from '@smallwei/avue';
import '@smallwei/avue/lib/index.css';
app.use(Avue)
// https工具
import {http} from './utils/https';
app.config.globalProperties.$https = http
// 时间工具
// 引入之后 用法 (new Date()).Format('yyyyMMdd')  传入格式 hh:mm:ss  只支持24小时制
import  '@/utils/newdate.js'

// 引入全局websocket
// import mqtt from "mqtt";
// 引入全局websocket   window.mqtt
import  "@/utils/mqtt.min";
let mqtt = window.mqtt;
// 链接地址
import {SOCKET_SERVICE} from "@/utils/mqtt";
let mqttServer = null;
try {
  // 随机一个用户id
  let userId = new Date().Format("yyyyMMddhhmmssSSS");
  console.log(mqtt)
    mqttServer = mqtt.connect(SOCKET_SERVICE, {
    username: "chrelyonly",
    password: "chrelyonly"
  });
  // 用户订阅id
  const singleTopic = "/userId" + userId;
  // 在线人数订阅地址
  const onlineUserNum = "/onlineUserNum";

  mqttServer.on("connect", () => {
    console.log("连接成功");
    // 订阅用户状态
    mqttServer.subscribe(singleTopic, err => {
      if (!err) {
        console.log("订阅成功:" + singleTopic);
      }
    });
    // 订阅在线人数
    mqttServer.subscribe(onlineUserNum, err => {
      if (!err) {
        console.log("订阅成功:" + onlineUserNum);
      }
    });
  });
  // mqttServer.on("message", (topic, message) => {
  //   console.log("收到消息" + message);
  //   this.messageCallback(message);
  // });
}catch (e) {
  console.log(e);
  console.log("mqtt连接失败");
}
app.config.globalProperties.$mqttServer = mqttServer
// ************************************自用组件库************************************
// components
for (const i in components) {
  app.component(i, components[i])
}

app.use(Router).mount('#app')
